<?php

/**
 * Actions for Metadata and Metastrings
 * 
 * @author Pablo
 * @since 1.0
 */

/******************************************************
 *					  METASTRINGS					  *
 ******************************************************/

/**
 * Checks if a metastring exists already
 * @param $string
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_metastring_exists($string){
	return (FALSE!==blg_metastring_get_id($string));
}

/**
 * Add a new metastring if does not exists already.
 * @param $string
 * @return $id Metastring's ID
 * @author Pablo
 * @since 1.0
 */
function blg_metastring_add($string){
	if(FALSE===($id=blg_metastring_get_id($string))){
		$query= blg_dbschema_query_buid(blg_dbschema_insert_metastring(array(
			'string' => $string	
		)));
		$result= blg_dbschema_insert($query);
		
		if($result->success)
			return (int) $result->new_id;
			
		return FALSE;
	}

	return (int) $id;
}

/**
 * Get a metastring string
 * @param $id Metastring's ID
 * @return string
 * @author Pablo
 * @since 1.0
 */
function blg_metastring_get_string($id){
	$query= blg_dbschema_query_buid(blg_dbschema_select_metastrings(
		array('id' => $id),
		array('string')
	));
	$result= blg_dbschema_select($query);
	
	if($result->num_rows==1){
		return $result->{0}['string'];
	}
	
	return FALSE;
}

/**
 * Get a metastring ID
 * @param $string Metastring's string
 * @return int ID
 * @author Pablo
 * @since 1.0
 */
function blg_metastring_get_id($string){
	$query= blg_dbschema_query_buid(blg_dbschema_select_metastrings(
		array('string' => $string),
		array('id')
	));
	$result= blg_dbschema_select($query);
	
	if($result->num_rows==1){
		return $result->{0}['id'];
	}	
	
	return FALSE;
}

/******************************************************
 *						METADATA					  *
 ******************************************************/

/**
 * Check if a metadata exists for a entity
 * @param $entity_guid
 * @param $name
 * @return FALSE|int Returns metadata ID if exists, FALSE otherwise.
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_exists($entity_guid, $name){
	if(FALSE!==($metadata=blg_metadata_get_entity($entity_guid, $name)))
		return $metadata['id'];
		
	return FALSE;
}

/**
 * Add metadata to an entiy
 * 
 * @param int $entity_guid
 * @param string $name
 * @param string $value
 * @param (integer|text) $value_type
 * @param int $owner_guid (optional, default is $entity_guid value)
 * @param int $access_id (optional, default is ACCESS_DEFAULT)
 * @param int $time_created (optional)
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_add($entity_guid, $name, $value, $value_type= NULL, $owner_guid=NULL, $access_id=ACCESS_DEFAULT, $time_created= NULL){
	blg_TRANSACTION();
	
	$name_id= blg_metastring_add($name);
	$value_id= blg_metastring_add($value);
	
	if(FALSE===$name_id || FALSE===$value_id){
		blg_ROLLBACK();
		return FALSE;
	}
	
	if(empty($owner_guid)) $owner_guid= $entity_guid;
	if(empty($time_created)) $time_created= time();
	if(empty($value_type)) $value_type= (is_int($value) || is_numeric($value) || v_is_numeric($value))?'integer':'text';		

	if(!in_array($value_type, array('integer', 'text'))){
		blg_ROLLBACK();
		return FALSE;
	}
		
	$query= blg_dbschema_query_buid(blg_dbschema_insert_metadata(array(
		'entity_guid' 	=> $entity_guid,
		'name_id' 		=> $name_id,
		'value_id' 		=> $value_id,
		'value_type' 	=> $value_type,
		'owner_guid' 	=> $owner_guid,
		'access_id' 	=> $access_id,
		'time_created'	=> $time_created,
		'enabled'		=> 'yes'
	)));
	$result= blg_dbschema_insert($query);
	
	if($result->success){
		blg_COMMIT();
		return $result->new_id;
	}
	
	blg_ROLLBACK();
	return FALSE;	
}

/**
 * Update metadata value
 * @param $id
 * @param $value
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_update_value($id, $value, $value_type=NULL){
	blg_TRANSACTION();
	
	$value_id= blg_metastring_add($value);

	if(FALSE===$value_id){
		blg_ROLLBACK();
		return FALSE;
	}

	if(empty($value_type)) $value_type= (is_int($value) || is_numeric($value) || v_is_numeric($value))?'integer':'text';
	
	$query= blg_dbschema_query_buid(blg_dbschema_update_metadata(
		array('id' => $id),
		array('value_id' => $value_id, 'value_type' => $value_type)
	));
	$result= blg_dbschema_update($query);
	
	if($result->success){
		blg_COMMIT();
		return TRUE;
	}
	
	blg_ROLLBACK();
	return FALSE;	
}

/**
 * Update metadata access value
 * @param $id
 * @param $acess_id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_update_acess($id, $access_id=ACCESS_DEFAULT){
	blg_TRANSACTION();
	
	$query= blg_dbschema_query_buid(blg_dbschema_update_metadata(
		array('id' => $id),
		array('acess_id' => $access_id)
	));
	$result= blg_dbschema_update($query);
	
	if($result->success){
		blg_COMMIT();
		return TRUE;
	}
	
	blg_ROLLBACK();
	return FALSE;	
}

/**
 * Remove medatada
 * @param $id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_delete($id){
	$query= blg_dbschema_query_buid(blg_dbschema_update_metadata(
		array('id' => $id),
		array('enabled' => 'no')
	));
	$result= blg_dbschema_update($query);
	return ($result->success && $result->affected_rows>0 );
}

/**
 * Remove entity's medatada
 * @param $id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_delete_entity($entity_guid){
	$query= blg_dbschema_query_buid(blg_dbschema_update_metadata(
		array('entity_guid' => $entity_guid),
		array('enabled' => 'no')
	));
	$result= blg_dbschema_update($query);
	return $result->success;
}

/**
 * Get a metadata by id
 * @param $id
 * @return (Array|FALSE) The metadata, FALSE in case of failure
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_get($id){
	$query= blg_dbschema_query_buid(blg_dbschema_select_metadata(array('id' => $id)));
	$result= blg_dbschema_select($query);
	if($result->success && $result->num_rows>0)
		return $result->{0};		
	
	return FALSE;
}

/**
 * Retrive metadata of an entity by name
 * @param int $entity_guid
 * @param string $name
 * @return mixed Metadata
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_get_entity($entity_guid, $name){
	$query= blg_dbschema_query_buid(blg_dbschema_select_metadata(array(
		'entity_guid' => $entity_guid,
		'name' => $name
	)));
	$result= blg_dbschema_select($query);
	if($result->success && $result->num_rows>0)
		return $result->{0};		
	
	return FALSE;
}

/**
 * Retrives metadata of an owner by name
 * @param int $entity_guid
 * @param string $name
 * @return mixed Metadata
 * @author Pablo
 * @since 1.0
 */
function blg_metadata_get_owner($owner_guid, $name){
	$query= blg_dbschema_query_buid(blg_dbschema_select_metadata(array(
		'owner_guid' => $owner_guid,
		'name' => $name
	)));
	$result= blg_dbschema_select($query);
	if($result->success && $result->num_rows>0)
		return $result->{0};		
	
	return FALSE;
}


